package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl  implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    //上传科室
    @Override
    public void save(Map<String, Object> paramMap) {
        //1把map转化成Department
        //把map转化json串
        String depJsonStr = JSONObject.toJSONString(paramMap);
        //把json串转化成对象
        Department department = JSONObject.parseObject(depJsonStr, Department.class);
        //2根据医院编码，科室编码查询科室信息
        Department departmentExist =
                departmentRepository.getByHoscodeAndDepcode(
                        department.getHoscode(),department.getDepcode()
                );
        //3判断是更新，新增
        if(departmentExist!=null){
            //更新
            department.setId(departmentExist.getId());
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else{
            //新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //带条件带分页获取科室列表
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //1创建分页对象pageable
        //1.1排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //1.2创建分页对象 0为第一页
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //2创建查询条件模板example
        //2.1创建查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //2.2创建匹配器，如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //2.2创建查询条件模板
        Example<Department> example = Example.of(department,matcher);
        //3进行分页查询
        Page<Department> pages = departmentRepository.findAll(example, pageable);

        return pages;
    }

    //根据医院编码、科室编码删除科室信息
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编码、科室编码查询科室信息
        Department department =
                departmentRepository.getByHoscodeAndDepcode(
                        hoscode,depcode);
        if(department!=null){
            //根据id删除
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编码查询科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //1 创建最终返回对象
        List<DepartmentVo> result = new ArrayList<>();
        //2根据医院编码查询所有科室
        //2.1创建查询条件对象
        Department department = new Department();
        department.setHoscode(hoscode);
        //2.2创建查询模板
        Example<Department> example = Example.of(department);
        //2.3查询数据
        List<Department> departmentList = departmentRepository.findAll(example);

        //3处理科室集合，根据大科室id进行划分，用流式计算
        //map 分析：key是bigcode，value是当前大科室下小科室集合
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //4 遍历数据，封装大科室信息
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            DepartmentVo bigDeptVo = new DepartmentVo();
            bigDeptVo.setDepcode(entry.getKey());
            bigDeptVo.setDepname(entry.getValue().get(0).getBigname());
            //5封装小科室信息
            //5.1创建封装小科室集合
            List<DepartmentVo> deptVoList = new ArrayList<>();
            //5.2获取小科室集合查询结果
            List<Department> deptList = entry.getValue();
            //5.3遍历deptList，获取数据进行封装deptVoList
            for (Department dept : deptList) {
                DepartmentVo departmentVo = new DepartmentVo();
                BeanUtils.copyProperties(dept,departmentVo);
                deptVoList.add(departmentVo);
            }
            //6小科室集合存入大科室对象
            bigDeptVo.setChildren(deptVoList);
            //7大科室对象存入返回集合
            result.add(bigDeptVo);
        }
        return result;
    }

    //根据医院编码、科室编码获取科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        //根据医院编码、科室编码查询科室信息
        Department department =
                departmentRepository.getByHoscodeAndDepcode(
                        hoscode,depcode);
        if(department!=null){
            return department.getDepname();
        }
        return "";
    }

    //根据医院编码、科室编码获取科室信息
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department =
                departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }


}
